from projects.models import FeatureGroup, Feature, FeatureDetail, Release
from io import open

def createFeatureGroup(release, input):
    featureGroup = FeatureGroup(name=input, description=" ", release=release)
    featureGroup.save()
    return featureGroup

def createFeature(featureGroup, input):
    priority = 1
    if input.endswith('P2'):
        priority = 2
    elif input.endswith('P3'):
        priority = 3
    f = Feature(name=input, fgroup=featureGroup, priority=priority)
    f.save()
    return f

def createFeatureDetail(feature, input):
    featureDetail = FeatureDetail(summary=input, feature=feature)
    featureDetail.save()
    return featureDetail

def parseWikiDoc(release, input):
    lines = input.splitlines()
    fgroup = None
    feature = None
    
    for line in lines:
        if line.startswith('='):
            fgroup = createFeatureGroup(release, line.strip('= '))
        else:
            if line.startswith('**'):
                createFeatureDetail(feature, line.strip('* '))
            else:
                if line.startswith('*'):
                    feature = createFeature(fgroup, line.strip('* '))


release = Release.objects.get(pk=1)

f = open('/tmp/wikidoc', 'r')

input = f.read()

f.close()

parseWikiDoc(release, input)
